<!DOCTYPE html>
<html>
<head>
    <include file="Include:meta"/>
    <title>项目过程文件-目的地运营集团数据管理平台</title>
    <include file="Include:style"/>
</head>
<body>
<include file="Include:header"/>
<include file="Include:left"/>
<div id="main">
    <div class="crumbs">
        <div class="crumbs-text">项目 > [{$result['project_num']}]{$result['project_name']} > 项目过程文件</div>
        <div class="right">
            <a href="javascript:;" class="btn btn-add">添加项目过程文件</a>
        </div>
    </div>
    <div id="content">
        <include file="header"/>
        <div class="table-scrollx">
            <table class="table table-bordered table-hover">
                <tr class="table-header fixed-table-header">
                    <td width="200">文件名称</td>
                    <td width="100">
                        <select name="type" class="form-control w100">
                            <option value="-10">文件分类</option>
                            <foreach name="ptype_list" item="vo">
                                <option value="{$vo['id']}"
                                <if condition="$type eq $vo['id']"> selected</if>
                                >{$vo['second_name']}</option>
                            </foreach>
                        </select>
                    </td>
                    <td width="200">附件</td>
                    <td width="84">文件日期</td>
                    <td width="350">说明</td>
                    <td width="84">添加人(日期)</td>
                    <td width="50">管理</td>
                </tr>
                <if condition="$result_file eq NULL">
                    <tr>
                        <td class="text-center">暂无相关内容</td>
                    </tr>
                </if>
                <foreach name="result_file" item="vo">
                    <tr>
                        <td>{$vo['name']}</td>
                        <td>{$vo['type_name']}</td>
                        <td>
                            <if condition="$vo['file_url'] neq NULL">{$vo['file_name']}<a href="{$vo['file_url']}"
                                                                                          class="ml5"
                                                                                          data-toggle="tooltip"
                                                                                          data-placement="right"
                                                                                          title="下载"><i
                                    class="glyphicon glyphicon-download-alt"></i></a></if>
                        </td>
                        <td>{$vo['file_date']}</td>
                        <td>{$vo['remark']}</td>
                        <td><span
                            <if condition="$vo['update_user'] neq NULL"> data-toggle="tooltip" data-placement="left"
                                title="最后操作人(时间):{$vo['update_user']}({$vo['update_date']})"
                            </if>
                            >{$vo['add_user']}({$vo['add_date']|substr=0,10})</span></td>
                        <td class="nowrap">
                            <a href="javascript:;" class="btn-edit" mid="{$vo['id']}" data-toggle="tooltip"
                               data-placement="left" title="编辑"><i class="glyphicon glyphicon-edit"></i></a>
                            <a href="javascript:;" class="btn-delete ml5" mid="{$vo['id']}" action="delete"
                               data-toggle="tooltip" data-placement="left" title="删除"><i
                                    class="glyphicon glyphicon-trash"></i></a>
                        </td>
                    </tr>
                </foreach>
            </table>
        </div>
    </div>
</div>
<!--弹框-->
<div class="modal fade" id="edit-modal">
    <div class="modal-dialog w600">
        <div class="modal-content">
            <div class="modal-header">
                <h4></h4>
                <i class="fa fa-times" aria-hidden="true" data-dismiss="modal"></i>
            </div>
            <div class="modal-body" style="line-height:30px;">
                <table class="table table-bordered">
                    <tr>
                        <td class="text-right" width="74"><strong><span class="asterisk">*</span>文件名称</strong></td>
                        <td>
                            <input type="text" class="form-control name w400" autocomplete="off"/>
                            <span class="error-item">必填项</span>
                        </td>
                    </tr>
                    <tr>
                        <td class="text-right"><strong><span class="asterisk">*</span>文件分类</strong></td>
                        <td>
                            <select class="form-control classify classify_select" autocomplete="off"
                                    style="width:195px">
                                <option value="-10" selected>请选择</option>
                                <foreach name="ptype_list" item="vo">
                                    <option value="{$vo['id']}">{$vo['second_name']}</option>
                                </foreach>
                            </select>
                            <input type="text" name="type_input" class="form-control select-add-text ml5"
                                   style="width:195px" autocomplete="off">
                            <span class="error-item">必填项</span>
                            <span class="error-item">该文件分类已存在</span>
                        </td>
                    </tr>
                    <tr>
                        <td class="text-right"><strong>附件</strong></td>
                        <td>
                            <div class="input-group w300 left">
                                <input type="text" class="form-control file-name" placeholder="请上传附件"
                                       autocomplete="off">
                                <span class="input-group-btn">
					        		<button class="btn file-submit" type="button">上传</button>
						    	</span>
                            </div>
                            <button class="btn file-delete ml5" type="button">删除</button>
                            <input type="hidden" class="file-url" autocomplete="off">
                            <span class="error-item">请上传附件</span>
                            <span class="error-item">文件名必填</span>
                        </td>
                    </tr>
                    <tr>
                        <td class="text-right"><strong><span class="asterisk">*</span>文件日期</strong></td>
                        <td>
                            <input type="text" class="form-control Wdate w100 date" onclick="WdatePicker()" readonly
                                   autocomplete="off"/>
                            <span class="error-item">必填项</span>
                        </td>
                    </tr>
                    <tr>
                        <td class="text-right"><strong>说明</strong></td>
                        <td>
                            <textarea class="form-control w400 explain" autocomplete="off"></textarea>
                        </td>
                    </tr>
                </table>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-affirm">确认</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>
<include file="Include:javascript"/>
<script type="text/javascript">
    $(function () {
        $(".classify_select").on('change', function () {
            classify();
        });
        classify();
        //删除项目过程文件
        $(".btn-delete").ajaxModal({
            "title_text": "确认提示框",
            "body_text": "您确定要删除吗？",
            "url": "__APP__/Project/project_files_status",
            "data": {"id": "mid", "action": "action"}
        });

        KindEditor.ready(function (K) {
            //上传附件
            var editorFile = K.editor();
            $('.file-submit').on('click', function () {
                var parents = $(this).parents(".input-group");
                editorFile.loadPlugin('insertfile', function () {
                    document.cookie = "file_name=project_files";
                    document.cookie = "file_type=ppt,pptx,pdf,doc,docx,jpg,jpeg,png,xls,xlsx,txt,rar,zip";
                    editorFile.plugin.fileDialog({
                        clickFn: function (url, title) {
                            parents.nextAll(".error-item").hide();
                            parents.find(".file-name").val(title).prop("readonly", false);
                            parents.nextAll(".file-url").val(url);
                            parents.nextAll(".file-delete").show();
                            editorFile.hideDialog();
                        }
                    });
                });
            });
        });

        //删除附件
        $(".file-delete").click(function () {
            $(this).hide();
            $(this).nextAll(".error-item").hide();
            $(this).nextAll(".file-url").val("");
            $(this).prevAll(".input-group").find(".file-name").val("").prop("readonly", true);
            $(this).parents("tr").find(".required-text2").next(".error-item").hide();
        });

        //添加
        $(".btn-add").on("click", function () {
            var modal = $("#edit-modal");
            modal.find(".modal-header h4").text("添加项目过程文件");
            modal.find(".file-name").prop("readonly", true);
            modal.find(".file-delete").hide();
            modal.modal();
        });

        //编辑
        $(".btn-edit").on("click", function () {
            var mid = $(this).attr("mid"),
                modal = $("#edit-modal");
            modal.find(".modal-header h4").text("编辑项目过程文件");
            modal.find(".btn-affirm").attr({"mid": mid});
            $.ajax({
                type: "POST",
                async: false,
                dataType: "json",
                data: {"id": mid},
                url: "__APP__/Project/project_files_ajax",
                success: function (data) {
                    modal.find(".name").val(data.name);
                    modal.find(".classify").val(data.type).select2();
                    modal.find(".file-name").val(data.file_name);
                    modal.find(".file-url").val(data.file_url);
                    modal.find(".date").val(data.file_date);
                    modal.find(".explain").val(data.remark);
                    modal.modal();
                    classify();
                    if (data.file_url) {
                        modal.find(".file-name").prop("readonly", false);
                        modal.find(".file-delete").show();
                    } else {
                        modal.find(".file-name").prop("readonly", true);
                        modal.find(".file-delete").hide();
                    }
                }
            });
        });

        //确定
        $("#edit-modal").find(".btn-affirm").on("click", function () {
            var tag = true,
                mid = $(this).attr("mid"),
                action,
                modal = $("#edit-modal");
            if (!mid) {
                mid = "";
                action = "add";
            } else {
                action = "edit";
            }
            modal.find(".classify_select").each(function () {
                var $this = $(this),
                    val = $this.val(),
                    text = $this.nextAll(".select-add-text").val();
                if (val == -10 && text == "") {
                    $this.siblings(".error-item").eq(0).show();
                    tag = false;
                } else {
                    $this.find("option").each(function () {
                        if ($(this).text() == text) {
                            $this.siblings(".error-item").eq(1).show()
                            tag = false;
                            return false;
                        }
                    });
                }
            });

            modal.find(".name,.date").each(function () {
                var val = $(this).val();
                if (val == "" || /^\s+$/.test(val)) {
                    $(this).next(".error-item").show();
                    tag = false;
                }
            });
            modal.find(".file-name").each(function () {
                val = $(this).val();
                if ((val == "" || /^\s+$/.test(val)) && !$(this).prop("readonly")) {
                    $(this).parents(".input-group").nextAll(".error-item").eq(1).show();
                    tag = false;
                }
            });

            if (tag) {
                var name = modal.find(".name").val(),
                    classify = modal.find(".classify").val(),
                    file_name = modal.find(".file-name").val(),
                    file_url = modal.find(".file-url").val(),
                    date = modal.find(".date").val(),
                    type_input = modal.find("[name='type_input']").val(),
                    pid = "{$result['pid']}",
                    explain = modal.find(".explain").val();
                modal.hide();
                $.ajax({
                    type: "POST",
                    async: false,
                    dataType: "json",
                    data: {
                        "action": action,
                        "id": mid,
                        "pid": pid,
                        "name": name,
                        "type": classify,
                        "type_input": type_input,
                        "file_name": file_name,
                        "file_url": file_url,
                        "file_date": date,
                        "remark": explain
                    },
                    url: "__APP__/Project/project_files_status",
                    success: function (data) {
                        success_pop(data.status);
                    }
                });
            }
        });

        //隐藏错误提示
        $('.select-add-text').on("focus", function () {
            $(this).nextAll(".error-item").hide();
        });
        $("#edit-modal").find(".classify").on("change", function () {
            $(this).nextAll(".error-item").hide();
        });
        $("#edit-modal").find(".name,.date").on("focus", function () {
            $(this).nextAll(".error-item").hide();
        });
        $("#edit-modal").find(".file-name").on("focus", function () {
            $(this).parents(".input-group").nextAll(".error-item").hide();
        });

        //取消
        $("#edit-modal").on('hidden.bs.modal', function () {
            $(this).find(".error-item").hide();
            $(this).find(".name,.file-name,.file-url,.date,.explain").val("");
            $(this).find("select").val(-10).select2();
            $(this).find(".btn-affirm").removeAttr("mid");
        });

        $(document).on("change", "[name='type']", function () {
            var val = $(this).val();
            window.location.href = "__APP__/CrmProjects/project_files/pid/{$result_file['pid']}/type/" + val;
        });
    });

    function classify() {
        if ($(".classify_select").val() == -10) {
            $("[name='type_input']").show();
        } else {
            $("[name='type_input']").val('').hide();
        }
    }
</script>
</body>
</html>